package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.BaseService;
import com.nexus.core.sys.entity.SysOrgMenu;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.mapper.SysOrgMenuMapper;
import com.nexus.core.sys.dto.SysOrgMenuDTO;
import com.nexus.service.sys.service.mapstruct.SysOrgMenuWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 机构菜单Service
 * @author luzhengning
 * @version 2024-04-19
 */
//@DS("erp_test_db")
@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysOrgMenuService extends BaseService<SysOrgMenuMapper, SysOrgMenu,SysOrgMenuDTO,SysOrgMenuWrapper> {

    
    @Autowired
	private SysOrgMenuWrapper sysOrgMenuWrapperImpl;

	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public SysOrgMenuDTO findById(String id) {
		return baseMapper.findById ( id );
	}

	/**
	 * 自定义分页检索
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	public IPage <SysOrgMenuDTO> findPage(Page <SysOrgMenuDTO> page, QueryWrapper queryWrapper) {
		queryWrapper.eq ("a.deleted", 0 ); // 排除已经删除
		queryWrapper.eq ("menu.deleted", 0 ); // 排除已经删除
		return  baseMapper.findList (page, queryWrapper);
	}

	/**
	 * 根据机构ID查询
	 * @Date 2024/4/19 10:14
	 * @Author luzhengning
	 **/
	public List<SysOrgMenuDTO> findByOrg(String orgId){
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq("a.org",orgId);
		IPage<SysOrgMenuDTO> page = findPage(new Page<>(1, 5000), queryWrapper);
		if(ObjectUtils.isNotEmpty(page.getRecords())){
			return page.getRecords();
		}
		return null;
	}

	public SysOrgMenuDTO findByOrgAndMenu(String orgId,String menuId){
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq("a.org",orgId);
		queryWrapper.eq("a.menu",menuId);
		IPage<SysOrgMenuDTO> page = findPage(new Page<>(1, 1), queryWrapper);
		if(ObjectUtils.isNotEmpty(page.getRecords())&&page.getRecords().size()>0){
			return page.getRecords().getFirst();
		}
		return null;
	}
	public List<SysOrgMenuDTO> findByOrgAndMenus(List<String> orgIds,List<String> menuIds){
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.in("a.org",orgIds);
		queryWrapper.in("a.menu",menuIds);
		IPage<SysOrgMenuDTO> page = findPage(new Page<>(1, 10000), queryWrapper);
		return page.getRecords();
	}


	/**
	 * 根据机构和菜单删除
	 * @Date 2024/4/19 11:41
	 * @Author luzhengning
	 **/
	public boolean deleteByOrgAndMenu(String orgId,List<String> menuIds){
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq("org",orgId);
		queryWrapper.in("menu",menuIds);
 		boolean remove = remove(queryWrapper);
		return remove;
	}

}
